package org.cli.admin.cli;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jline.ArgumentCompletor;
import jline.Completor;
import jline.ConsoleReader;
import jline.SimpleCompletor;

import com.beust.jcommander.JCommander;
import com.google.inject.Inject;
import com.google.inject.Provider;

public class ConsoleReaderProvider implements Provider<ConsoleReader>{

	@Inject
	private JCommander commander;
	
	@Override
	public ConsoleReader get() {
		ConsoleReader reader;
		try {
			reader = new ConsoleReader();
		} catch (IOException e) {
			throw new RuntimeException("Console reader could not be initialized.");
		}

		Map<String, JCommander> commands = commander.getCommands();
		Set<String> strings = commands.keySet();

		List<Completor> completors = new ArrayList<Completor>();
		completors.add(new SimpleCompletor(strings.toArray(new String[strings.size()])));
		reader.addCompletor(new ArgumentCompletor(completors));
		return reader;
	}

}
